using Microsoft.AspNetCore.Mvc;
using ProduceBasicData.Api.Services;
using ProduceBasicData.ErrorCode;

namespace ProduceBasicData.Api.Controllers
{
    /// <summary>
    /// AI使用示例控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class AIExampleController : ControllerBase
    {
        private readonly IAIService _aiService;
        private readonly ILogger<AIExampleController> _logger;

        public AIExampleController(
            IAIService aiService,
            ILogger<AIExampleController> logger)
        {
            _aiService = aiService;
            _logger = logger;
        }

        /// <summary>
        /// MES系统智能问答示例
        /// </summary>
        /// <param name="question">用户问题</param>
        /// <returns>AI回答</returns>
        [HttpGet("mes-qa")]
        public async Task<ActionResult<ApiResult<string>>> MESQuestionAnswer([FromQuery] string question)
        {
            try
            {
                if (string.IsNullOrEmpty(question))
                {
                    return BadRequest(new ApiResult<string>
                    {
                        Code = ApiEnum.Failed,
                        Msg = "问题不能为空"
                    });
                }

                // 构建MES系统相关的上下文
                var context = @"
MES系统功能模块：
1. 生产管理：生产计划、工单管理、生产报工
2. 物料管理：物料信息、库存管理、BOM管理
3. 质量管理：质检单、质量报告、不合格品处理
4. 设备管理：设备维护、设备状态监控
5. 人员管理：员工信息、权限管理、考勤管理
";

                var reply = await _aiService.SendMessageAsync(question, context, "gpt-3.5-turbo");

                return Ok(new ApiResult<string>
                {
                    Code = ApiEnum.Success,
                    Msg = "问答成功",
                    Data = reply
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "MES问答时发生错误");
                return StatusCode(500, new ApiResult<string>
                {
                    Code = ApiEnum.Failed,
                    Msg = "服务器内部错误"
                });
            }
        }

        /// <summary>
        /// 生产数据分析示例
        /// </summary>
        /// <param name="data">生产数据</param>
        /// <returns>分析结果</returns>
        [HttpPost("production-analysis")]
        public async Task<ActionResult<ApiResult<string>>> ProductionAnalysis([FromBody] ProductionDataRequest request)
        {
            try
            {
                if (string.IsNullOrEmpty(request.Data))
                {
                    return BadRequest(new ApiResult<string>
                    {
                        Code = ApiEnum.Failed,
                        Msg = "数据不能为空"
                    });
                }

                var systemPrompt = @"你是一个生产数据分析专家，请分析以下生产数据并提供专业的分析报告：
1. 生产效率分析
2. 质量指标分析
3. 设备利用率分析
4. 改进建议";

                var analysis = await _aiService.GenerateReplyAsync(request.Data, systemPrompt);

                return Ok(new ApiResult<string>
                {
                    Code = ApiEnum.Success,
                    Msg = "数据分析完成",
                    Data = analysis
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "生产数据分析时发生错误");
                return StatusCode(500, new ApiResult<string>
                {
                    Code = ApiEnum.Failed,
                    Msg = "服务器内部错误"
                });
            }
        }

        /// <summary>
        /// 智能文档生成示例
        /// </summary>
        /// <param name="request">文档生成请求</param>
        /// <returns>生成的文档</returns>
        [HttpPost("generate-document")]
        public async Task<ActionResult<ApiResult<string>>> GenerateDocument([FromBody] DocumentRequest request)
        {
            try
            {
                if (string.IsNullOrEmpty(request.Content))
                {
                    return BadRequest(new ApiResult<string>
                    {
                        Code = ApiEnum.Failed,
                        Msg = "内容不能为空"
                    });
                }

                var systemPrompt = $@"你是一个专业的文档编写助手，请根据以下要求生成{request.DocumentType}文档：
1. 格式规范，结构清晰
2. 内容专业，逻辑性强
3. 语言简洁明了
4. 符合行业标准";

                var document = await _aiService.GenerateReplyAsync(request.Content, systemPrompt);

                return Ok(new ApiResult<string>
                {
                    Code = ApiEnum.Success,
                    Msg = "文档生成成功",
                    Data = document
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "文档生成时发生错误");
                return StatusCode(500, new ApiResult<string>
                {
                    Code = ApiEnum.Failed,
                    Msg = "服务器内部错误"
                });
            }
        }
    }

    /// <summary>
    /// 生产数据请求
    /// </summary>
    public class ProductionDataRequest
    {
        /// <summary>
        /// 生产数据
        /// </summary>
        public string Data { get; set; } = string.Empty;
    }

    /// <summary>
    /// 文档请求
    /// </summary>
    public class DocumentRequest
    {
        /// <summary>
        /// 文档类型
        /// </summary>
        public string DocumentType { get; set; } = "报告";

        /// <summary>
        /// 内容
        /// </summary>
        public string Content { get; set; } = string.Empty;
    }
}




